/**
 * @author Isabella Nake (isabella.nake@usi.ch)
 * @author Nicolas Lusa (lusan@usi.ch)
 */
package ch.usi.inf.pf2.gui.tool;

import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import ch.usi.inf.pf2.circuit.Component;
import ch.usi.inf.pf2.circuit.Input;
import ch.usi.inf.pf2.circuit.Value;
import ch.usi.inf.pf2.gui.CircuitFrame;
import ch.usi.inf.pf2.gui.Model;
import ch.usi.inf.pf2.time.Schedule;
import ch.usi.inf.pf2.time.SetValueOfInputEvent;

public class WaveformTool extends Tool{
	
	private Input currentInput;
	private ArrayList<Value> values;
	private boolean validInput;
	private final CircuitFrame circuitFrame;
	
	
	public WaveformTool(final CircuitFrame circuitFrame, final Model model){
		super(model, "Select this tool and click on an input to set a waveform.", "../icons/waveform.png", "../icons/waveformBg.png");
        currentInput = null;
        values = new ArrayList<Value>();
        validInput = true;
        
        this.circuitFrame = circuitFrame;
        
	}
	
	public void mouseClicked(final MouseEvent ev) {
		model.clearSelection();
		currentInput = null;
		validInput = true;
		values = new ArrayList<Value>();
        final int x = ev.getX();
        final int y = ev.getY();
        final Component shape = getCircuit().findShapeAt(x, y);
        if (shape instanceof Input){
        	currentInput = ((Input) shape);
        	ImageIcon icon = new ImageIcon(getClass().getResource("../icons/WaveformDialog.png"));
        	//String waveform = JOptionPane.showInputDialog("Enter waveform", currentInput.getWaveform());
        	String waveform = (String)JOptionPane.showInputDialog( null, "Enter waveform", "", JOptionPane.QUESTION_MESSAGE, icon, null, currentInput.getWaveform());
        	
        	if(waveform != null) {
	        	for(int i=0; i < waveform.length(); i++){
	        		if(waveform.charAt(i) == '1'){
	        			values.add(Value.TRUE);
	        		} else if (waveform.charAt(i) == '0'){
	        			values.add(Value.FALSE);
	        		} else{
	        			validInput = false;
	        			break;
	        		}
	        	}
	        	
	        	System.out.println(waveform);
	        	
	        	if(validInput){
	        		circuitFrame.getEditor().removeAll();
	        		for(int i=0; i<values.size(); i++){
	        				new SetValueOfInputEvent(values.get(i), currentInput, i);
	        		}
	            	model.shapeChanged();
	            	currentInput.setWaveform(waveform);
	            	
	            	currentInput.getLabel().setText(currentInput.getName() + ": " + currentInput.getWaveform());
	            	
	            	for(Input input : model.getInputs()) {
	            		circuitFrame.setEditor(input.getLabel());
	            		
	            		values = new ArrayList<Value>();
	            		if(input != currentInput) {
	            	        	for(int i=0; i < input.getWaveform().length(); i++){
	            	        		if(input.getWaveform().charAt(i) == '1'){
	            	        			values.add(Value.TRUE);
	            	        		} else if (input.getWaveform().charAt(i) == '0'){
	            	        			values.add(Value.FALSE);
	            	        		}
	            	        	}
	            	        	for(int i=0; i<values.size(); i++){
	    	        				new SetValueOfInputEvent(values.get(i), input, i);
	            	        	}
		        		}
	            		
	            	}
	            	
	            	
	            	circuitFrame.getEditor().revalidate();
	            	Schedule.resetStep();
	        	} else {
	        		//circuitFrame.getEditor().append("INVALID INPUT INSERTED");
	        	}
        	
        	}
        }
        else{
        }
	}
	
	

}
